package cn.ekuma.epos.order;

import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JCheckBox;
import javax.swing.LayoutStyle.ComponentPlacement;

import cn.ekuma.epos.bean.util.CustomerHelper;
import cn.ekuma.epos.bean.util.erp.OrderHelper;

import com.openbravo.bean.Location;
import com.openbravo.bean.erp.Order;
import com.openbravo.bean.erp.OrderType;
import com.openbravo.pos.base.AppLocal;

import java.awt.BorderLayout;
import java.util.List;

public class JOrderDetail extends JPanel {
	private JTextField jCustomerName;
	private JTextField jLocationName;
	private JTextField jLocation1Name;
	private JTextField jOrderTypeName;
	private JTextField jOrderCreateTime;
	private JTextField jConsultName;
	private JRadioButton jInStore1;
	private JRadioButton jOutStore1;
	private JRadioButton jInStore;
	private JRadioButton jOutStore;
	private JCheckBox jReverse;

	List<Location> locations;
	
	/**
	 * Create the panel.
	 */
	public JOrderDetail() {
		initComponents();
	}
	private void initComponents() {
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		panel.setRequestFocusEnabled(false);
		panel.setPreferredSize(new Dimension(450, 151));
		panel.setFocusable(false);
		add(panel);
		
		JLabel label = new JLabel();
		label.setText(AppLocal.getIntString("label.ordertype.name"));
		
		JLabel label_1 = new JLabel();
		label_1.setText(AppLocal.getIntString("label.order.location1"));
		
		JLabel label_2 = new JLabel();
		label_2.setText(AppLocal.getIntString("label.order.location"));
		
		JLabel label_3 = new JLabel();
		label_3.setText(AppLocal.getIntString("label.order.customer"));
		
		jCustomerName = new JTextField();
		jCustomerName.setRequestFocusEnabled(false);
		jCustomerName.setFocusable(false);
		jCustomerName.setEditable(false);
		
		jLocationName = new JTextField();
		jLocationName.setRequestFocusEnabled(false);
		jLocationName.setFocusable(false);
		jLocationName.setEditable(false);
		
		jLocation1Name = new JTextField();
		jLocation1Name.setRequestFocusEnabled(false);
		jLocation1Name.setFocusable(false);
		jLocation1Name.setEditable(false);
		
		jInStore1 = new JRadioButton();
		jInStore1.setText(AppLocal.getIntString("list.OrderType.STORE_In"));
		jInStore1.setRequestFocusEnabled(false);
		jInStore1.setFocusable(false);
		jInStore1.setEnabled(false);
		
		jOutStore1 = new JRadioButton();
		jOutStore1.setText(AppLocal.getIntString("list.OrderType.STORE_Out"));
		jOutStore1.setRequestFocusEnabled(false);
		jOutStore1.setFocusable(false);
		jOutStore1.setEnabled(false);
		
		jInStore = new JRadioButton();
		jInStore.setText(AppLocal.getIntString("list.OrderType.STORE_In"));
		jInStore.setRequestFocusEnabled(false);
		jInStore.setFocusable(false);
		jInStore.setEnabled(false);
		
		jOutStore = new JRadioButton();
		jOutStore.setText(AppLocal.getIntString("list.OrderType.STORE_Out"));
		jOutStore.setRequestFocusEnabled(false);
		jOutStore.setFocusable(false);
		jOutStore.setEnabled(false);
		
		jOrderTypeName = new JTextField();
		jOrderTypeName.setRequestFocusEnabled(false);
		jOrderTypeName.setFocusable(false);
		jOrderTypeName.setEditable(false);
		
		JLabel label_4 = new JLabel();
		label_4.setText(AppLocal.getIntString("label.order.createTime"));
		
		jOrderCreateTime = new JTextField();
		jOrderCreateTime.setRequestFocusEnabled(false);
		jOrderCreateTime.setFocusable(false);
		jOrderCreateTime.setEditable(false);
		
		jReverse = new JCheckBox();
		jReverse.setText(AppLocal.getIntString("label.ordertype.reverse"));
		jReverse.setRequestFocusEnabled(false);
		jReverse.setFocusable(false);
		jReverse.setEnabled(false);
		
		JLabel label_5 = new JLabel();
		label_5.setText(AppLocal.getIntString("label.order.consult"));
		
		jConsultName = new JTextField();
		jConsultName.setRequestFocusEnabled(false);
		jConsultName.setFocusable(false);
		jConsultName.setEditable(false);
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGap(0, 550, Short.MAX_VALUE)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
								.addComponent(label, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 54, Short.MAX_VALUE)
								.addComponent(label_2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(label_3, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
								.addComponent(jCustomerName, GroupLayout.DEFAULT_SIZE, 472, Short.MAX_VALUE)
								.addGroup(gl_panel.createSequentialGroup()
									.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
										.addComponent(jLocationName, GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
										.addComponent(jLocation1Name, GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE))
									.addPreferredGap(ComponentPlacement.RELATED)
									.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
										.addGroup(gl_panel.createSequentialGroup()
											.addComponent(jInStore1)
											.addPreferredGap(ComponentPlacement.UNRELATED)
											.addComponent(jOutStore1))
										.addGroup(gl_panel.createSequentialGroup()
											.addComponent(jInStore)
											.addPreferredGap(ComponentPlacement.UNRELATED)
											.addComponent(jOutStore)))
									.addGap(10))
								.addGroup(gl_panel.createSequentialGroup()
									.addComponent(jOrderTypeName, GroupLayout.PREFERRED_SIZE, 191, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(label_4, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(jOrderCreateTime, GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(jReverse)))
							.addContainerGap())
						.addComponent(label_5, GroupLayout.PREFERRED_SIZE, 57, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(58)
							.addComponent(jConsultName, GroupLayout.DEFAULT_SIZE, 482, Short.MAX_VALUE))))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGap(0, 199, Short.MAX_VALUE)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(label)
						.addComponent(jReverse)
						.addComponent(jOrderTypeName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(jOrderCreateTime, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label_4))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(label_3)
						.addComponent(jCustomerName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
								.addComponent(label_2)
								.addComponent(jLocationName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
								.addComponent(label_1)
								.addComponent(jLocation1Name, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(10)
							.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
								.addComponent(jInStore)
								.addComponent(jOutStore))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
								.addComponent(jInStore1)
								.addComponent(jOutStore1))))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addComponent(label_5)
						.addComponent(jConsultName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(58, Short.MAX_VALUE))
		);
		panel.setLayout(gl_panel);
	}

	public void printOrderState(Order order) {
		if (Order.ORDERBEARING_REVERSE == order.getOrderType())
			jOrderTypeName.setText(order.getDefineType().getReverseName());
		else
			jOrderTypeName.setText(order.getDefineType().getName());
		jOrderCreateTime.setText(OrderHelper.printDate(order));
		jReverse.setSelected(Order.ORDERBEARING_REVERSE == order.getOrderType());
		if (order.getCustomer() != null) {
			jCustomerName
					.setText(CustomerHelper.printName(order.getCustomer()));
		} else {
			jCustomerName.setText(null);
		}

		OrderType oType = order.getDefineType();
		int loctionCount = oType.getLocationRefCount();
		switch (loctionCount) {
		case 0:
			jLocationName.setText(null);
			jInStore.setSelected(false);
			jOutStore.setSelected(false);

			jLocation1Name.setText(null);
			jInStore1.setSelected(false);
			jOutStore1.setSelected(false);
			break;
		case 1:
			jLocationName.setText(getLocationName(order.getLocationRef()));
			jInStore.setSelected(oType.getInOutType() == OrderType.STORE_In);
			jOutStore.setSelected(oType.getInOutType() == OrderType.STORE_Out);

			jLocation1Name.setText(null);
			jInStore1.setSelected(false);
			jOutStore1.setSelected(false);
			break;
		case 2:
			jLocationName.setText(getLocationName(order.getLocationRef()));
			jInStore.setSelected(oType.getInOutType() == OrderType.STORE_First_In_Second_Out);
			jOutStore
					.setSelected(oType.getInOutType() == OrderType.STORE_First_Out_Second_In);

			jLocation1Name.setText(getLocationName(order.getLocationRef()));
			jInStore1
					.setSelected(oType.getInOutType() == OrderType.STORE_First_Out_Second_In);
			jOutStore1
					.setSelected(oType.getInOutType() == OrderType.STORE_First_In_Second_Out);
			break;
		}
		int consultType = oType.getConsultType();
		if (OrderType.CONSULTTYPE_Null == consultType) {
			jConsultName.setText(null);
		} else if (OrderType.CONSULTTYPE_Order == consultType) {
			jConsultName.setText(AppLocal
					.getIntString("list.OrderType.CONSULTTYPE_Order")
					+ ":"
					+ order.getConsultRef());
		} else if (OrderType.CONSULTTYPE_TimeSliceStore == consultType) {
			jConsultName.setText(AppLocal
					.getIntString("list.OrderType.CONSULTTYPE_TimeSliceStore")
					+ ":" + order.getConsultRef());
		} else if (OrderType.CONSULTTYPE_RealStore == consultType) {
			jConsultName.setText(AppLocal
					.getIntString("list.OrderType.CONSULTTYPE_RealStore"));
		}
		
	}
	public List<Location> getLocations() {
		return locations;
	}
	public void setLocations(List<Location> locations) {
		this.locations = locations;
	}
	
	private String getLocationName(String id) {
		for (Location loc : locations) {
			if (loc.getID().equalsIgnoreCase(id))
				return loc.getName() + "-" + loc.getAddress();
		}
		return id;
	}
}
